package com.ccp.dev.system.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.Convert;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.system.model.IndexLayout;
import com.ccp.dev.system.service.IndexLayoutService;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 首页布局 信息操作处理
 *
 * @author wanghepeng
 * @date 2019-03-21
 */
@Controller
@Action(ownermodel = SysAuditModelType.HOME_MANAGEMENT)
@RequestMapping(UrlConstants.PROJECT_ROOT+UrlConstants.PROJECT_MODULE_SYSTEM+"/indexLayout")
public class IndexLayoutController extends SysBaseController {

	@Resource
	private IndexLayoutService indexLayoutService;
	
    /**
     * 跳转到首页布局列表页
     * @return html文件前缀
     */
    @GetMapping("/view/list")
    @Action(description="首页布局列表页面跳转",exectype = "页面跳转日志")
    public String viewIndexLayoutList(){
        return "sysIndexConfig/sysIndexLayout/list";
    }
    /**
     * 跳转到首页布局编辑页,包括新增和修改 
     * @return html文件前缀
     */
    @GetMapping("/view/add")
    @Action(description="首页布局添加页面跳转",exectype = "页面跳转日志")
    public String viewIndexLayoutAdd(String id, Model model){
    	model.addAttribute("id", id);
        return "sysIndexConfig/sysIndexLayout/addEdit"; 
    }

    /**
     * 跳转到首页布局查看页
     * @return html文件前缀
     */
    @GetMapping("/view/view")
    @Action(description="首页布局查看页面跳转",exectype = "页面跳转日志")
    public String viewIndexLayoutView(String id, Model model){
    	model.addAttribute("id", id);
        return "sysIndexConfig/sysIndexLayout/view";
    }
    
    /**
     * 新增及修改首页布局
     * @param indexLayout	首页布局对象
     * @ResponseBody
     * @return 返回ResultData对象
     */
    @PostMapping(UrlConstants.MODULE_FUNCTION_SAVE)
    @Action(description="编辑首页布局保存",detail="<#if StringUtil.isEmpty(id)>新增<#else>更新</#if>首页布局<#if success>【${name}】成功<#else>失败</#if>",exectype = "编辑日志")
    @ResponseBody
    public ResultData save(IndexLayout indexLayout) {
        indexLayout.setTemplateHtml(StringEscapeUtils.unescapeHtml4(indexLayout.getTemplateHtml()));
        try{
            SysAuditThreadLocalHolder.putParamerter("success", true);
            SysAuditThreadLocalHolder.putParamerter("id", indexLayout.getId());
            if (StringUtils.isEmpty(indexLayout.getName())) {
                SysAuditThreadLocalHolder.putParamerter("success", false);
                return ResultData.error("名称不能为空");
            }
            SysAuditThreadLocalHolder.putParamerter("name", indexLayout.getName());
            IndexLayout i = new IndexLayout();
            i.setName(indexLayout.getName());
            List<IndexLayout> list = indexLayoutService.selectIndexLayoutList(i);

            int count = 0;
            // 有id为新增，否则为修改
            if (StringUtils.isEmpty(indexLayout.getId())) {	// 新增
                // 校验名称是否存在
                if (BeanUtils.isNotEmpty(list)) {
                    SysAuditThreadLocalHolder.putParamerter("success", false);
                    return ResultData.error("名称已存在");
                }
                indexLayout.setId(UUIDUtils.getUUIDFor32());
                indexLayoutService.insertIndexLayout(indexLayout);
            } else {	// 修改
                // 如果有id不同并且名称相同的，则修改失败
                if (BeanUtils.isNotEmpty(list)) {
                    for (IndexLayout layout : list) {
                        if (!layout.getId().equals(indexLayout.getId()) && layout.getName().equals(indexLayout.getName())) {
                            SysAuditThreadLocalHolder.putParamerter("success", false);
                            return ResultData.error("名称已存在");
                        }
                    }
                }
                indexLayoutService.updateIndexLayout(indexLayout);
            }
            return ResultData.success();
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return ResultData.error();
        }
    }
    
    /**
     * 删除首页布局
     * @return 返回ResultData对象
     */
    @PostMapping(UrlConstants.MODULE_FUNCTION_DELETE)
    @ResponseBody
    @Action(description="删除首页布局",detail="删除首页布局<#if success><#list name as item>【${item}】</#list>成功<#else>失败</#if>",exectype = "删除日志")
    public ResultData del(String ids) {
        try{
            SysAuditThreadLocalHolder.putParamerter("success", true);
            if (StringUtils.isEmpty(ids)) {
                SysAuditThreadLocalHolder.putParamerter("success", false);
                return ResultData.error("没有id传入");
            }
            SysAuditThreadLocalHolder.putParamerter("name",indexLayoutService.queryIndexLayoutNameByIds(Convert.toStrArray(ids)));
            indexLayoutService.deleteIndexLayoutByIds(ids);
            return ResultData.success();
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return ResultData.error();
        }
    }
    
    /**
     * 取得首页布局列表
     * @param request HttpServletRequest
     * @return ResultData
     */
    @ResponseBody
    @GetMapping(UrlConstants.MODULE_FUNCTION_LIST)
    @Action(description="查询首页布局列表",detail="查询首页布局列表<#if success>成功<#else>失败</#if>",exectype = "查询日志")
    public ResultData list(HttpServletRequest request) {
        try{
            SysAuditThreadLocalHolder.putParamerter("success", true);
            QueryFilter queryFilter=new QueryFilter(request);
            List<IndexLayout> list=indexLayoutService.queryList(queryFilter);
            return getResultDataSuccess("operation.success",queryFilter.getPageBean().getTotalCount(),list);
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return ResultData.error();
        }
    }
    
    /**
     * 取得一条首页布局记录
     * @return ResultData
     */
    @ResponseBody
    @GetMapping(UrlConstants.MODULE_FUNCTION_GET)
    @Action(description="查询首页布局明细",detail="查询首页布局明细<#if success>【${name}】成功<#else>失败</#if>",exectype = "查询日志")
    public ResultData get(String id) {
        try{
            SysAuditThreadLocalHolder.putParamerter("success", true);
            if (StringUtils.isEmpty(id)) {
                SysAuditThreadLocalHolder.putParamerter("success", false);
                return ResultData.error("id不能为空");
            }
            IndexLayout indexLayout = indexLayoutService.getOneById(id);
            SysAuditThreadLocalHolder.putParamerter("name", indexLayout.getName());
            return ResultData.success(indexLayout);
        }catch (Exception e){
            SysAuditThreadLocalHolder.putParamerter("success", false);
            return ResultData.error();
        }
    }
}
